﻿using CommonNet48.Extend;
using DevExpress.Utils.MVVM;
using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DevUIUtils.MVVM.Views
{
    /// <summary>
    /// MVVM用户交互数据安全验证扩展类
    /// </summary>
    public static class ViewCheckUIDataSafeEx
    {
        /// <summary>
        /// 非空验证
        /// </summary>
        /// <param name="edit">验证输入控件对象</param>
        /// <param name="e">MVVM绑定事件参数</param>
        /// <param name="errText">错误提示字符串</param>
        public static void ValidateNotNull(this BaseEdit edit, BindingValidatingEventArgs e, string errText = "不能为空！")
        {
            edit.CausesValidation = false;
            if (e.Value == null)
            {
                e.IsValid = false;
                edit.ErrorText = errText;
                return;
            }
            if (string.IsNullOrWhiteSpace(e.Value?.ToString()))
            {
                e.IsValid = false;
                edit.ErrorText = errText;
                return;
            }
            e.IsValid = true;
            edit.ErrorText = string.Empty;
        }

        /// <summary>
        /// 十六进制Byte格式验证
        /// </summary>
        /// <param name="edit">验证输入控件对象</param>
        /// <param name="e">MVVM绑定事件参数</param>
        /// <param name="errText">错误提示字符串</param>
        public static void ValidateHexByte(this BaseEdit edit, BindingValidatingEventArgs e, string errText = "请输入正确的格式(如0xAA,FA)！")
        {
            edit.CausesValidation = false;
            string sVal = e.Value?.ToString();
            if (string.IsNullOrWhiteSpace(sVal))
            {
                e.IsValid = false;
                edit.ErrorText = errText;
            }
            else
            {
                if (!sVal.Replace("0x", "").ConvertToNum<byte>(System.Globalization.NumberStyles.HexNumber).HasValue)
                {
                    e.IsValid = false;
                    edit.ErrorText = errText;
                }
                else
                {
                    e.IsValid = true;
                    edit.ErrorText = string.Empty;
                }
            }
        }

        /// <summary>
        /// 十六进制ushort格式验证
        /// </summary>
        /// <param name="edit">验证输入控件对象</param>
        /// <param name="e">MVVM绑定事件参数</param>
        /// <param name="errText">错误提示字符串</param>
        public static void ValidateHexUInt16(this BaseEdit edit, BindingValidatingEventArgs e, string errText = "请输入正确的格式(如0x00AA,A88A)！")
        {
            edit.CausesValidation = false;
            string sVal = e.Value?.ToString();
            if (string.IsNullOrWhiteSpace(sVal))
            {
                e.IsValid = false;
                edit.ErrorText = errText;
            }
            else
            {
                if (!sVal.Replace("0x", "").ConvertToNum<ushort>(System.Globalization.NumberStyles.HexNumber).HasValue)
                {
                    e.IsValid = false;
                    edit.ErrorText = errText;
                }
                else
                {
                    e.IsValid = true;
                    edit.ErrorText = string.Empty;
                }
            }
        }
    }
}
